package org.example.erat.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.dto.CourseDTO;
import org.example.erat.entity.Course;
import org.example.erat.exception.ResourceNotFoundException;
import org.example.erat.repository.ClassRepository;
import org.example.erat.repository.CourseRepository;
import org.example.erat.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CourseServiceImpl implements CourseService {

    private final CourseRepository courseRepository;
    private final ClassRepository classRepository;

    @Override
    @Transactional
    public CourseDTO createCourse(CourseDTO courseDTO) {
        log.info("创建课程: {}", courseDTO.getName());

        Course course = Course.builder()
                .name(courseDTO.getName())
                .teacher(courseDTO.getTeacher())
                .experimentCount(courseDTO.getExperimentCount())
                .build();

        Course savedCourse = courseRepository.save(course);
        return convertToDTO(savedCourse);
    }

    @Override
    @Transactional
    public CourseDTO updateCourse(Long id, CourseDTO courseDTO) {
        log.info("更新课程: {}", id);

        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("课程不存在: " + id));

        course.setName(courseDTO.getName());
        course.setTeacher(courseDTO.getTeacher());
        course.setExperimentCount(courseDTO.getExperimentCount());

        Course updatedCourse = courseRepository.save(course);
        return convertToDTO(updatedCourse);
    }

    @Override
    @Transactional
    public void deleteCourse(Long id) {
        log.info("删除课程: {}", id);

        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("课程不存在: " + id));

        courseRepository.delete(course);
    }

    @Override
    public CourseDTO getCourse(Long id) {
        log.info("获取课程: {}", id);

        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("课程不存在: " + id));

        return convertToDTO(course);
    }

    @Override
    public List<CourseDTO> getAllCourses() {
        log.info("获取所有课程");

        List<Course> courses = courseRepository.findAll();
        return courses.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 将课程实体转换为DTO
     * @param course 课程实体
     * @return 课程DTO
     */
    private CourseDTO convertToDTO(Course course) {
        int classCount = classRepository.findByCourse(course).size();

        return CourseDTO.builder()
                .id(course.getId())
                .name(course.getName())
                .teacher(course.getTeacher())
                .experimentCount(course.getExperimentCount())
                .classCount(classCount)
                .build();
    }
}
